//
//  GZWelcomeViewController.swift
//  sinaweibo
//
//  Created by mac book pro on 16/3/23.
//  Copyright © 2016年 finish. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
class GZWelcomeViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        //读取沙盒缓存图片
        if let avatar_large = GZUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            self.iconImageView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        //请求加载用户信息
        GZUserAccountViewModel.sharedInstance.loadUserInfos { (error) -> () in
            if error != nil{
                print("加载出错")
                return
            }
            
            if let avatar_large = GZUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
                self.iconImageView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
        }
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
        
    }
    private func startAnimation(){
        //约束动画
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(-(UIScreen.mainScreen().bounds.height-160))
        }
        UIView.animateWithDuration(2.0, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(defalutanimationDuration, animations: { () -> Void in
                    self.titleLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        print("动画完成,回到首页")
                        AppDelegate.switchRootViewController(GZMainViewController())
                })
        }
    }
    private func prepareUI(){
        view.addSubview(backgroundImageView)
        view.addSubview(iconImageView)
        view.addSubview(titleLabel)
        
        //约束
        
        backgroundImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.trailing.equalTo(self.view)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.width.height.equalTo(85)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
        }
        //title
        titleLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.iconImageView)
            make.top.equalTo(self.iconImageView.snp_bottom).offset(16)
        }
        
    }
    //MARK: - 懒加载
    private lazy var iconImageView: UIImageView = {
        var iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconImageView.layer.cornerRadius =  42.5
        iconImageView.clipsToBounds = true
        return iconImageView
        }()
    private lazy var titleLabel: UILabel = {
        var titleLabel = UILabel()
        titleLabel.text = "欢迎归来"
        titleLabel.font = UIFont.systemFontOfSize(14)
        titleLabel.sizeToFit()
        titleLabel.alpha = 0.0
        return titleLabel
        }()
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
}
